import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:part_time_job_pro/common/style/color.dart';
import 'package:part_time_job_pro/common/widgets/custom_appbar.dart';
import 'package:part_time_job_pro/pages/service/lang_setting/view.dart';
import 'package:part_time_job_pro/pages/service/logic.dart';
import 'package:part_time_job_pro/pages/service/theme_setting/view.dart';

class ServicePage extends StatelessWidget {
  ServicePage({super.key});

  //路由地址
  static const String routeName = "/service";
  final logic = Get.find<ServiceLogic>();
  final state = Get.find<ServiceLogic>().state;

  // 设置项列表
  late final List<SettingItem> settingsItems = [
    SettingItem(
      title: '语言设置',
      trailingWidget: Icon(Icons.arrow_forward, size: 20),
      onTap: () {
        Get.toNamed(LangSettingPage.routeName)?.then((index) {
          if (index != null) {
            logic.changeLanguage(index);
          }
        });
      },
    ),
    SettingItem(title: '修改密码', trailingWidget: Icon(Icons.arrow_forward, size: 20)),
    SettingItem(
      title: '设置主题',
      trailingWidget: Icon(Icons.arrow_forward, size: 20),
      onTap: () {
        Get.toNamed(ThemeSettingPage.routeName)?.then((index) {
          if (index != null) {
            logic.changeTheme(index);
          }
        });
      },
    ),
    SettingItem(title: '消息设置', trailingWidget: Icon(Icons.arrow_forward, size: 20)),
    SettingItem(title: '注销账号', trailingWidget: Icon(Icons.arrow_forward, size: 20)),
    SettingItem(title: '用户协议', trailingWidget: Icon(Icons.arrow_forward, size: 20)),
    SettingItem(title: '隐私协议', trailingWidget: Icon(Icons.arrow_forward, size: 20)),
    SettingItem(title: '检查版本', trailingWidget: Icon(Icons.arrow_forward, size: 20)),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar.customAppBar(hasLeading: false, title: "主题设置"),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 设置项列表
              ListView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                itemCount: settingsItems.length,
                itemBuilder: (context, index) {
                  final item = settingsItems[index];
                  return ListTile(
                    title: Text(item.title),
                    trailing: item.trailingWidget,
                    onTap: () {
                      // 处理点击事件
                      if (item.onTap != null) {
                        item.onTap!();
                      }
                    },
                  );
                },
              ),

              // 退出登录按钮
              Container(
                width: double.infinity,
                padding: EdgeInsets.symmetric(horizontal: 16.0),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(25.0),
                  border: Border(top: BorderSide(color: Colors.grey[300]!, width: 1.0)),
                  color: AppColor.primaryBackground,
                ),
                child: TextButton(
                  child: Text('退出登录', style: TextStyle(color: Colors.white, fontSize: 16.0)),
                  onPressed: () {
                    // 处理退出登录逻辑
                    logic.loginOut();
                  },
                ),
              ),
              SizedBox(height: 50.0),
            ],
          ),
        ),
      ),
    );
  }
}

// 设置项数据模型
class SettingItem {
  final String title;
  final Widget? trailingWidget;
  final VoidCallback? onTap;

  SettingItem({required this.title, this.trailingWidget, this.onTap});
}
